{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[32minside  points # 238\u001b[0m\n",
      "\u001b[1m\u001b[31moutside points # 262\u001b[0m\n",
      "\u001b[1ma-sphericity: 0.006964348969187656\u001b[0m\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9dc046125cfe41459d0e17b9f0d64a5f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], axes_helper=1.0, background_color=16777215, camera=[0.0434704314547055…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Draw the PCA (Principal Component Analysis) ellipsoid that contains\n",
    "50% of a cloud of Points, then check if points are inside the surface.\n",
    "Extra info is stored in actor.info['sphericity'], 'va', 'vb', 'vc'.\n",
    "\"\"\"\n",
    "from vtkplotter import *\n",
    "import numpy as np\n",
    "embedWindow('k3d') # or k3d\n",
    "\n",
    "vp = Plotter()\n",
    "\n",
    "pts = np.random.randn(500, 3) # random gaussian point cloud\n",
    "\n",
    "act = pcaEllipsoid(pts, pvalue=0.5, pcaAxes=1) # act is a group of [ellipse, 3 axes]\n",
    "vp += act\n",
    "\n",
    "ipts = act.getActor(0).insidePoints(pts) # get the ellipse and select points inside mesh\n",
    "opts = act.getActor(0).insidePoints(pts, invert=True)\n",
    "vp += Points(ipts, c=\"g\")\n",
    "vp += Points(opts, c=\"r\")\n",
    "\n",
    "printc(\"inside  points #\", len(ipts), c='g')\n",
    "printc(\"outside points #\", len(opts), c='r')\n",
    "printc(\"a-sphericity:\", act.info[\"sphericity\"])\n",
    "vp.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "closePlotter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
